package com.haoyu.number.Demo.SingleLinkedList;

/**
 * @author 蒿雨
 * @create 2021-11-01 10:35
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        //测试
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(1, "小白");
        doubleLinkedList.list();
    }
}


class NodeD {
    public int no;
    public String name;
    public NodeD pre;
    public NodeD next;

    public NodeD(int no, String name, NodeD pre, NodeD next) {
        this.no = no;
        this.name = name;
        this.pre = pre;
        this.next = next;
    }

    @Override
    public String toString() {
        return "NodeD{" +
                "no=" + no +
                ", name=" + name + '\'' +
                ", pre=" + pre +
                ", next=" + next +
                '}';
    }
}

class DoubleLinkedList {
    private NodeD header;
    private NodeD tail;

    public DoubleLinkedList() {
        //生成默认的头尾节点
        header = new NodeD(0, "", null, null);
        tail = new NodeD(0, "", header, null);
        header.next = tail;
        System.out.println(header);
        System.out.println(tail);

    }

    //添加方法
    public void add(int no, String name) {
        NodeD nodeD = new NodeD(no, name, null, null);
        System.out.println("添加");
        System.out.println("头节点" + header);
        System.out.println("尾节点" + tail);
        tail.pre.next = nodeD;
        nodeD.pre = tail.pre;
        System.out.println(tail);
        nodeD.next = tail;
        tail.pre = nodeD;
    }

    //非空判断
    public boolean isEmpty() {
        if (header.next == tail) {
            return true;
        }
        return false;
    }

    //打印输出
    public void list() {
        NodeD temp = header.next;
        if (isEmpty()) {
            System.out.println("链表为空");
            return;
        }
        while (temp.next != null) {
            System.out.println(1);
            System.out.println(temp);
            temp = temp.next;
        }
    }

}